package com.jbm.jbmsupplier.presenters;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.jbm.core.common.MainServer;
import com.jbm.jbmsupplier.activity.loadhomepage.factoryhome.LoadHomeFactory;
import com.jbm.jbmsupplier.modes.LoginInterface;
import com.jbm.jbmsupplier.net.NetIntent;
import com.jbm.jbmsupplier.shared.SharedPreferActivity;
import com.jbm.jbmsupplier.views.LoginViews;
import com.jbm.jbmsupplier.zp.service.MyService;
import com.jbm.onehome.common.OHInterface;

/**
 * Created by 家帮忙 on 2017/7/7.
 *
 *
 *
 *
 */

public class LoginActivitys implements LoginInterface {
    private Context context;
    private LoginViews loginViews;
    private SharedPreferActivity sharedPreferActivity;
    private boolean ISboo;
    private MyConnSock mycon=null;
    private IntentFilter filer;
    private final int LOGIN=0001;
    private LoadHomeFactory loadhomefactory;

    public LoginActivitys(Context context, LoginViews loginViews) {
        this.context = context;
        this.loginViews = loginViews;
    }
    public void setInitLogin(){//初始化数据
        initmode();
    }
    public void setLogin(String name,String pwd){//获取用户名密码
        setLoginmode(name,pwd);
    }


    @Override
    public void initmode() {
        sharedPreferActivity=new SharedPreferActivity(context,true);
        String allnamepwd= sharedPreferActivity.getNmaes();
        
        if (!allnamepwd.equals("")){
            loginViews.getLoginname(allnamepwd.substring(0,allnamepwd.indexOf(",")),  allnamepwd.substring(allnamepwd.indexOf(",")+1,allnamepwd.length()));
        }
        mycon=new MyConnSock();
        filer= new IntentFilter();
        filer .addAction("SOCKCONN");
        if (sharedPreferActivity.getBoolearNetConn()==false){
            ISboo=true;
            context.startService(new Intent(context, MyService.class));
            context.registerReceiver(mycon,filer) ;
        }
         loadhomefactory = new LoadHomeFactory();
    }
   private String names;
    private String pwds;
    @Override
    public void setLoginmode(String name, String pwd) {
        this.names=name;
        this.pwds=pwd;
        Thread thread=new Thread(runnable);
        thread.start();
    }
Runnable runnable=new Runnable() {
    @Override
    public void run() {
        NetIntent netintent = new NetIntent(handler);
        netintent.setLogin(names, pwds, LOGIN);
    }
};

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case LOGIN:
                    String login=(String) msg.obj;
                    if (login.equals("Login OK")){
                        sharedPreferActivity.setName(names,pwds);
                        sharedPreferActivity.setBoolearLogin(true);//判断是否登陆
                        sharedPreferActivity.setBoolearNetConn(true);//判断是否第一次连接服务器，如果登录成功就设为true 代表第一次链接成功
                        sharedPreferActivity.setLoginid(OHInterface.getG_server().login_id);
                        sharedPreferActivity.setUserName(OHInterface.getG_server().nickname);
                        MainServer.getG_server().setM_local_id(OHInterface.getG_server().login_id);
                        loadhomefactory.getLoagHomePage("2").LoadNewHome(context);//第二个代表管理端
//                       context. startActivity(new Intent(context,LoadHoadPageManage.class)/*.ptExtra("main","")*/);
/**
 * 登录成功后 获取这个人的权限
 */
//                        ModeRxjavaNet.Companion.getInstantMode().objects(Observable.create(new Observable.OnSubscribe<PrivilegeListEntity>() {
//                            @Override
//                            public void call(Subscriber<? super PrivilegeListEntity> subscriber) {
//                              subscriber.onNext(ModeRxjavaNet.Companion.getInstantMode().getOhInter().getPersonalPrivilegeList(true,0,OHInterface.getG_server().login_id));
//                            }
//                        })).subscribe(new Observer<PrivilegeListEntity>() {
//                            @Override
//                            public void onCompleted() {
//
//                            }
//
//                            @Override
//                            public void onError(Throwable e) {
//
//                            }
//
//                            @Override
//                            public void onNext(PrivilegeListEntity privilegeListEntity) {
//                                if (privilegeListEntity.m_code==200){
//                                     ArrayList<PrivilegeListEntity> arrPteson=new ArrayList<PrivilegeListEntity>();
//                                    if (privilegeListEntity.priList!=null) {
//                                        arrPteson.addAll(privilegeListEntity.priList);
//                                        new PtesonalPrivile().setArrPteson(arrPteson, context);//将所有的权限保存在本地
//
//                                    }


//                                }else{
//                                    loginViews.setError(privilegeListEntity.m_info);
//                                }
//
//                            }
//                        });

                        loginViews.setSucced();
                    }else if (login.equals("FAILED")){
                        loginViews.setError("请求网络超时");
                    }else if (login.equals("request timeout")){
                        loginViews.setError("请求超时");
                    }else if (login.equals("Not Connect")){
                        Toast.makeText(context, ISboo+"登陆后"+login, Toast.LENGTH_SHORT).show();
                        loginViews.setError("未连接网络，请联网后在进入");
                    }else{
                        loginViews.setError(login);
                    }
                    break;
            }


        }
    };

    class MyConnSock extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent)

        {
            if (intent.getStringExtra("comm").equals("2")){
            ISboo=false;
//            loginViews.getinitvies(ISboo);
        }else if (intent.getStringExtra("comm").equals("1")){
            ISboo=true;
//            loginViews.getinitvies(ISboo);
        }else if (intent.getStringExtra("comm").equals("3")){
            ISboo=true;
//            loginViews.getinitvies(ISboo);
        }
        }
    }
    public void setonDestroy() {//销毁当前页面
        if (mycon==null){
            return;}else
        if (mycon!=null)
        {if (ISboo){
            context.unregisterReceiver(mycon);}
        }
    }
}
